//
//  JSAlertHelper.m
//  QMUI_TEST
//
//  Created by mac on 17/4/10.
//  Copyright © 2017年 Apple. All rights reserved.
//

#import "JSAlertHelper.h"
#import "QMUIToastContentView.h"
#import "QMUITips.h"
@interface JSAlertHelper ()

//@property (nonatomic, strong) dispatch_source_t js_time;
//@property (nonatomic, assign) NSInteger count;
@end

@implementation JSAlertHelper
JSSingletonM

/*!
 *  显示文字提示框 点击mask隐藏
 *  @title      显示标题 可不传 默认为 【温馨提示】
 *  @content    提示内容 必须传
 */
+(void)JSAlert_showTitle:(NSString *) title content:(NSString *) content{
    if (!title) {
        title = @"温馨提示";
    }
    QMUIAlertController *alert = [QMUIAlertController alertControllerWithTitle:title message:content preferredStyle:QMUIAlertControllerStyleAlert];
    [alert showWithAnimated:YES];
    alert.shouldRespondMaskViewTouch = YES;
}

/*!
 *  显示文字提示框 
 *  @title      显示标题 可不传 默认为 【温馨提示】
 *  @content    提示内容 必须传
 *  @confrm     确定想要执行的操作 回调
 *  @cancel     取消想要执行的操作 回调
 */
+(void) JSALert_showTitle:(NSString *)  title
                  content:(NSString *)  content
                  confirm:(dispatch_block_t)   confirm
                   cancel:(dispatch_block_t)   cancel{
    if (!title) {
        title = @"温馨提示";
    }
    QMUIAlertController *alert = [QMUIAlertController alertControllerWithTitle:title message:content preferredStyle:QMUIAlertControllerStyleAlert];
    QMUIAlertAction *confirmAction = [QMUIAlertAction actionWithTitle:@"确定" style:QMUIAlertActionStyleDestructive handler:^(QMUIAlertAction *action) {
        if (confirm) {
            confirm();
        }
        [alert hideWithAnimated:YES];
    }];
    QMUIAlertAction *cancelAction = [QMUIAlertAction actionWithTitle:@"取消" style:QMUIAlertActionStyleDefault handler:^(QMUIAlertAction *action) {
        if (cancel) {
            cancel();
        }
        [alert hideWithAnimated:YES];
    }];
//    // 自定义属性
//    /*
//     
//     NSMutableDictionary *titleAttributs = [[NSMutableDictionary alloc] initWithDictionary:alertController.alertTitleAttributes];
//     titleAttributs[NSForegroundColorAttributeName] = UIColorWhite;
//     alertController.alertTitleAttributes = titleAttributs;
//     NSMutableDictionary *messageAttributs = [[NSMutableDictionary alloc] initWithDictionary:alertController.alertMessageAttributes];
//     messageAttributs[NSForegroundColorAttributeName] = UIColorMakeWithRGBA(255, 255, 255, 0.75);
//     alertController.alertMessageAttributes = messageAttributs;
//     alertController.alertHeaderBackgroundColor = UIColorBlue;
//     */
    [alert addAction:cancelAction];
    [alert addAction:confirmAction];
    [alert showWithAnimated:YES];
    
    
}
/*!
 *  显示文字 + 图片 提示框
 *  @title      显示标题 可不传 默认为 【温馨提示】
 *  @content    提示内容
 *  @contentView自定义view
 *  @confrm     确定想要执行的操作 回调
 *  @cancel     取消想要执行的操作 回调
 */
+(void) JSALert_showTitle:(NSString *)  title
                  content:(NSString *)  content
               contenView:(UIView *)    contentView
                  confirm:(dispatch_block_t)   confirm
                   cancel:(dispatch_block_t)   cancel{
    if (!title) {
        title = @"温馨提示";
    }
    QMUIAlertController *alert = [QMUIAlertController alertControllerWithTitle:title message:content preferredStyle:QMUIAlertControllerStyleAlert];
    QMUIAlertAction *confirmAction = [QMUIAlertAction actionWithTitle:@"确定" style:QMUIAlertActionStyleDestructive handler:^(QMUIAlertAction *action) {
        if (confirm) {
            confirm();
        }
        [alert hideWithAnimated:YES];
    }];
    QMUIAlertAction *cancelAction = [QMUIAlertAction actionWithTitle:@"取消" style:QMUIAlertActionStyleDefault handler:^(QMUIAlertAction *action) {
        if (cancel) {
            cancel();
        }
        [alert hideWithAnimated:YES];
    }];
    [alert addCustomView:contentView];
    [alert addAction:cancelAction];
    [alert addAction:confirmAction];
    [alert showWithAnimated:YES];
    
}
/*!
 *  显示 actionSeet 提示框
 *  @title      显示标题 可不传 默认为 【温馨提示】
 *  @content    提示内容
 *  @actions    传入需要执行的操作 QMUIAlertAction
 */
+(void) JSSeet_showActionSeet:(NSString *)title
                      content:(NSString *)content
                      actions:(NSArray<QMUIAlertAction *>*)actions{
    if (!title) {
        title = @"温馨提示";
    }
    QMUIAlertController *alert = [QMUIAlertController alertControllerWithTitle:title message:content preferredStyle:QMUIAlertControllerStyleActionSheet];
    for (QMUIAlertAction *action in actions) {
        [alert addAction:action];
    }
    [alert showWithAnimated:YES];
}
/*!
 *  显示加载框
 *  @text   描述文字
 *  @view   显示在那个view上
 *  @ainmation  是否有动画
 */
-(void)showLoadingText:(NSString *)text inView:(UIView *)view animation:(BOOL)animation{
    if (!view) {
        view = [UIApplication sharedApplication].keyWindow;
    }
    _tips = [self createTipsToView:view];
    QMUIToastContentView *contentView = (QMUIToastContentView *)_tips.contentView;
    contentView.minimumSize = CGSizeMake(90, 90);
    [_tips showLoading:text];
    
}
- (QMUITips *)createTipsToView:(UIView *)view{
    QMUITips *tips = [[QMUITips alloc] initWithView:view];
    [view addSubview:tips];
    tips.removeFromSuperViewWhenHide = YES;
    return tips;
}
/*! 隐藏 */
-(void)hideAnimated:(BOOL)aniamted{
    if (self.tips) {
        [self.tips hideAnimated:YES];
    }
}
/*!
 *  显示加载框
 *  @delay  显示时间
 *  @text   描述文字
 *  @view   显示在那个view上
 *  @ainmation  是否有动画
 */
+(void)showLoadingHideAfterDelay:(NSInteger)delay
                            text:(NSString *)text
                          inView:(UIView *)view
                       animation:(BOOL)aimation{
    if (!view) {
        view = [UIApplication sharedApplication].keyWindow;
    }
    [QMUITips showWithText:text inView:view hideAfterDelay:delay];
}

+(void)showSucceedHideAfterDelay:(NSInteger)delay
                            text:(NSString *)text
                          inView:(UIView *)view
                       animation:(BOOL)aimation{
    if (!view) {
    view = [UIApplication sharedApplication].keyWindow;
    }
    [QMUITips showSucceed:text inView:view hideAfterDelay:delay];
}
+(void)showFailHideAfterDelay:(NSInteger)delay
                         text:(NSString *)text
                       inView:(UIView *)view
                    animation:(BOOL)aimation{
    if (!view) {
        view = [UIApplication sharedApplication].keyWindow;
    }
    [QMUITips showError:text inView:view hideAfterDelay:delay];
}
@end
@implementation JSAlertHelper (action)
+(QMUIAlertAction *) caretQmuiAction:(NSString *)title
                               image:(UIImage *) image
                               style:(QMUIAlertActionStyle)style
                              action:(dispatch_block_t)action1{
   QMUIAlertAction *Alertaction = [QMUIAlertAction actionWithTitle:title style:style handler:^(QMUIAlertAction *action) {
       if (action1) {
           action1();
       }
   }];
    if (image) {
        [Alertaction.button setImage:image forState:UIControlStateNormal];
    }
    return Alertaction;
}


@end
